package xyz.lxbq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.lxbq.pojo.Depart;
import xyz.lxbq.pojo.TClass;
import xyz.lxbq.service.TClassService;
import xyz.lxbq.utils.PageUtils;
import xyz.lxbq.utils.ResultMessage;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author XuHu
 * @version 1.0
 * @date 2020/10/17 20:59
 */
@Controller
@Scope("prototype")
public class TClassController {

    @Autowired
    TClassService tClassService;
    /**
     * 获取所有班级信息
     */
    @RequestMapping("/getAllClass")
    @ResponseBody
    public List<TClass> getAllClass() {
        return tClassService.getAllClass();
    }

    @RequestMapping("/addTclass")
    @ResponseBody
    public ResultMessage addTclassdata(@Valid TClass tClass, BindingResult result, HttpSession httpSession, HttpServletResponse httpServletResponse, int majorId, String className, String classDate, String classTime, String classAddress){
        ResultMessage rm = null;

        int errorCount = result.getErrorCount();
        if(errorCount>0){
            List<FieldError> fieldErrors = result.getFieldErrors();
            //遍历其中所有的错误
            StringBuffer stringBuffer = new StringBuffer();

            for (FieldError fieldError : fieldErrors) {
                String msg=fieldError.getDefaultMessage();
                stringBuffer.append(msg);
            }
            rm = new ResultMessage(300,stringBuffer.toString());
        }else {
            TClass tClass1 = new TClass(majorId,className, classDate,classTime,classAddress);
            int i = tClassService.addClassdata(tClass1);
            if(i>0){
                rm = new ResultMessage(200,"添加成功");
                httpSession.setAttribute("TClass",tClass);
            }else {
                rm = new ResultMessage(300,"添加失败");
            }
        }
        return rm;
    }


    @RequestMapping("/Tclasspage/{pageIndex}/{pageSize}")
    public String getclasspage(@PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize, Model model){
        int selectcount = tClassService.selectcount();
        List<TClass> pageClass = tClassService.getPageClass((pageIndex - 1) * pageSize, pageSize);
        PageUtils pageUtils = new PageUtils(pageIndex, pageSize, selectcount, pageClass);
        model.addAttribute("pageUtils",pageUtils);
        return "ClassList";
    }

    @RequestMapping("/Tclassdel")
    @ResponseBody
    public ResultMessage delclass(int id){
        ResultMessage Mess = null;
        int delclass = tClassService.delclass(id);
        if(delclass>0){
            Mess = new ResultMessage(200,"删除成功");
        }else{
            Mess  = new ResultMessage(300, "删除失败");
        }

        return Mess;
    }
}
